package agency.tango.materialintro.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import agency.tango.materialintro.MaterialIntro;
import agency.tango.materialintro.ResourceTable;
import agency.tango.materialintroscreen.utils.ElementUtil;
import agency.tango.materialintroscreen.utils.LogUtil;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintro.slice
 * @ClassName: SplashAbilitySlice
 * @Description: 引导页, 会自动跳转（第一次自动跳转首页，否则只要APP进程存在，从后台再次进入直接跳转pageslider显示界面）
 * @CreateDate: 2021/4/12 14:34
 * @UpdateDate: 2021/4/12 14:34
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */
public class SplashAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_splash);
        getWindow().setStatusBarColor(new Color(ElementUtil.getColor(getContext(),
                ResourceTable.Color_statusBar_color)).getValue());
    }

    @Override
    public void onActive() {
        super.onActive();
        EventRunner eventRunner = EventRunner.create();
        EventHandler handler = new EventHandler(eventRunner);
        handler.postTask(() -> {
            try {
                Thread.sleep(500);
                jumpMain();
            } catch (InterruptedException e) {
                LogUtil.error(SplashAbilitySlice.this.getClass().getSimpleName(), e.toString());
            }

        });
    }

    private void jumpMain() {
        // 判断APP是在后台还是第一次登陆
        if (MaterialIntro.isIsFirstOpen()) {
            Intent secondIntent = new Intent();
            // 指定待启动FA的bundleName和abilityName
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("agency.tango.materialintro")
                    .withAbilityName("agency.tango.materialintro.MainAbility")
                    .build();
            secondIntent.setOperation(operation);
            startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
        } else {
            Intent secondIntent = new Intent();
            // 指定待启动FA的bundleName和abilityName
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("agency.tango.materialintro")
                    .withAbilityName("agency.tango.materialintro.IntroAbility")
                    .build();
            secondIntent.setOperation(operation);
            startAbility(secondIntent); // 通过AbilitySlice的startAbility接口实现启动另一个页面
        }
        terminate();
        terminateAbility();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
